package com.example.springboottx.controller;

import com.example.springboottx.bean.Book;
import com.example.springboottx.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@RestController
public class BookController {

    @Autowired
    private BookService bookService;

    @Autowired
    private RedisTemplate<String, String> template;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    RedisOperations<String, String> stringStringRedisOperations;


    @GetMapping("/book/all")
    public List<Book> findAllBooks() {
        ExecutorService executorService = Executors.newFixedThreadPool(3);
        for (int i = 0; i < 10; i++) {
            executorService.submit(() -> {
                bookService.findAllBooks();
            });
        }
        executorService.shutdown();
        return bookService.findAllBooks();
    }

    @GetMapping("/book/string/{id}")
    @ResponseBody
    public String findBookByIdUseString(@PathVariable(value = "id", required = true) String id) {
        Book book;
        System.out.println(id);
        String s = template.opsForValue().get("book." + id);
        System.out.println("s: " + s);
        if (s == null) {
            book = bookService.findBookById(Integer.parseInt(id));
            System.out.println("mysql: " + book);
            template.opsForValue().set("book." + id, book.toString());
            System.out.println("book: " + book);
            return "mysql: " + book.toString();
        } else {
            return "redis: " + s;
        }
    }


    //redis必须传入String类型
    @GetMapping("/book/hash/{id}")
    public String findBookByIdUseHash(@PathVariable("id") Integer id) {
        Map<Object, Object> bookMap = template.opsForHash().entries("book." + id);
        if (bookMap.isEmpty()) {
            Book book = bookService.findBookById(id);
            System.out.println("mysql: " + book);
            bookMap.put("name", book.getName());
            bookMap.put("price", book.getPrice().toString());
            bookMap.put("amount", book.getAmount().toString());
            bookMap.put("path", book.getPath());
            bookMap.put("publishDate", book.getPublishDate().toString());
            template.opsForHash().putAll("book." + id, bookMap);
            return "mysql: " + book;
        } else {
            return "redis: " + bookMap.toString();
        }
    }

    @GetMapping("/redis/test")
    public String redisTest() {
        template.opsForValue().set("myKey", "myValue");
        return template.opsForValue().get("myKey");
    }

}
